<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 渲染权限点列表 -->
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <!-- // 选中时绑定的数据字段(唯一) -->
    <!-- 权限点数据展示 -->
    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="quedin">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 树形结构
import { tranListToTreeData } from '@/utils/index.js'
// 获取权限列表 数据回填 分配权限
import { getPermissionList, getRoleDetailById, assignPerm } from '@/api/permission'

export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      id: '',
      roleId: [],
      // 树形结构
      permissionData: []
    }
  },
  created () {
    this.xgetPermissionList()
  },
  methods: {
    // 关闭弹窗
    closeAssignDialog () {
      this.$emit('update:showAssignDialog', false)
    },
    // 树形结构
    async xgetPermissionList () {
      const res = await getPermissionList()
      this.permissionData = tranListToTreeData(res)
    },
    // 获取回填的数据
    async xgetRoleDetailById (id) {
      this.id = id
      const { permIds } = await getRoleDetailById(id)
      console.log(permIds)
      this.permIds = permIds
      this.$refs.tree.setCheckedKeys(permIds)
    },
    async quedin () {
      //  {id:角色id, permIds:[] 所有选中的节点的id组成的数组}
      await assignPerm({ id: this.id, permIds: this.$refs.tree.getCheckedKeys() })
      this.$message.success('操作成功！')
      this.closeAssignDialog()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
